/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sims.gui.salesperson;

import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import sims.entity.Product;
import sims.manager_SalesPerson.Function;

/**
 *
 * @author vanthanh
 */
public class ItemList extends javax.swing.JPanel {

    /**
     * Creates new form DisplayItem
     */
   
    
    DefaultTableModel model = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int i, int i1) {
            return false;
        }
    };
    public ItemList() {
        initComponents();
        ArrayList<Product> arrProduct = new ArrayList<Product>();
        
        Function fun = new Function();
        arrProduct = fun.get_All_Item();
        
        String[] column = {"STT","CatName","ItemID","Item Name","Unit","Price","Quantity"};
        model.setColumnIdentifiers(column);

        int stt =0;
        for(Product pro : arrProduct)
        {
            stt++;
            String catName = fun.getCatName(Integer.parseInt(pro.getCatID()));
            Object[] row ={stt,catName,pro.getId(),pro.getProductName(),pro.getUnit(),pro.getPrice(),pro.getQuantity()};
            model.addRow(row);
        }
        jTable_ListAllItem.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_searchCat = new javax.swing.JTextField();
        txt_searchItem = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btn_searchItem = new javax.swing.JButton();
        btn_searchCat = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_ListAllItem = new javax.swing.JTable();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("List Item");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Search For category:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Search For Item :");

        btn_searchItem.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btn_searchItem.setText("OK");
        btn_searchItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_searchItemActionPerformed(evt);
            }
        });

        btn_searchCat.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btn_searchCat.setText("OK");
        btn_searchCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_searchCatActionPerformed(evt);
            }
        });

        jTable_ListAllItem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable_ListAllItem);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(179, 179, 179)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_searchCat)
                            .addComponent(txt_searchItem, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn_searchCat)
                            .addComponent(btn_searchItem))))
                .addContainerGap(110, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_searchCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_searchCat))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_searchItem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btn_searchItem)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btn_searchItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_searchItemActionPerformed
        ArrayList<Product> arrProduct = new ArrayList<Product>();
        Function fun = new Function();
        DefaultTableModel model = new DefaultTableModel();
        String[] column = {"STT","CatName","ItemID","Item Name","Unit","Price","Quantity","QuantityEOD"};
        model.setColumnIdentifiers(column);
        String searchItem = txt_searchItem.getText();
        arrProduct = fun.search_Item(searchItem);
        int stt =0;
        for(Product pro : arrProduct)
        {
            stt++;
            String catName = fun.getCatName(Integer.parseInt(pro.getCatID()));
            Object[] row ={stt,catName,pro.getId(),pro.getProductName(),pro.getUnit(),pro.getPrice(),pro.getQuantity(),pro.getQuantityEOD()};
            model.addRow(row);
        }
        jTable_ListAllItem.setModel(model);
    }//GEN-LAST:event_btn_searchItemActionPerformed

    private void btn_searchCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_searchCatActionPerformed
        ArrayList<Product> arrProduct = new ArrayList<Product>();
        Function fun = new Function();
        DefaultTableModel model = new DefaultTableModel();
        String[] column = {"STT","CatName","ItemID","Item Name","Unit","Price","Quantity","QuantityEOD"};
        model.setColumnIdentifiers(column);
        String searchCat = txt_searchCat.getText();
        arrProduct = fun.search_Category(searchCat);
        int stt =0;
        for(Product pro : arrProduct)
        {
            stt++;
            String catName = fun.getCatName(Integer.parseInt(pro.getCatID()));
            Object[] row ={stt,catName,pro.getId(),pro.getProductName(),pro.getUnit(),pro.getPrice(),pro.getQuantity(),pro.getQuantityEOD()};
            model.addRow(row);
        }
        jTable_ListAllItem.setModel(model);
    }//GEN-LAST:event_btn_searchCatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_searchCat;
    private javax.swing.JButton btn_searchItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_ListAllItem;
    private javax.swing.JTextField txt_searchCat;
    private javax.swing.JTextField txt_searchItem;
    // End of variables declaration//GEN-END:variables
}
